package com.str.list;
import java.io.*;

public class ListTest {
	
  private static List createList(int i) {
  	if (i == 1) return new ArrayList();
    if (i == 2) return new LinkedList();
  	return null;
  }     
    
  public static void main(String args[]) throws IOException {
    BufferedReader stdin = new BufferedReader(
                           new InputStreamReader(System.in));

    while (true) {
      System.out.println("1. Create Array List");
      System.out.println("2. Create Linked List");    
      System.out.print("\tEnter your choice(Enter to quit): ");     
      
      String inputLine = stdin.readLine();      
      if (inputLine.equals("")) {
        System.out.println("End of run.");
        System.exit(0);
      }
      
      int choice = Integer.parseInt(inputLine);
      List strList = createList(choice);
      if (strList == null) {
        System.out.println("Invalid choice.");
        continue;
      } 
    
      while (true) {
        System.out.println("-------------------------");
        System.out.println("     1. Clear");      
        System.out.println("     2. Add");    
        System.out.println("     3. Append");
        System.out.println("     4. Remove");
        System.out.println("     5. Find");
        System.out.print("\tEnter your choice(Enter to quit): ");

        inputLine = stdin.readLine();
        if (inputLine.equals("")) break;
        choice = Integer.parseInt(inputLine);

        switch (choice) {
          case 1: 
            System.out.println("Cleared.");
            strList.clear();
            break;
          
          case 2: 
            System.out.print("Enter insert position: ");
            inputLine = stdin.readLine();
            int i = Integer.parseInt(inputLine);
            System.out.print("Enter a string: ");
            Object val = stdin.readLine();
            strList.add(i, val);
            break;
        
          case 3: 
            System.out.print("Enter a string: ");
            val = stdin.readLine();
            strList.add(val);
            break;
        
          case 4: 
            if (strList.isEmpty())
              System.out.println("List is empty.");
            else {
              System.out.print("Enter remove position: ");
              inputLine = stdin.readLine();
              i = Integer.parseInt(inputLine);
              val = strList.remove(i);
              System.out.println(val + " has been deleted.");
            }
            break;
          
          case 5: 
            System.out.print("Enter a string: ");
            val = stdin.readLine();
            i = strList.indexOf(val);
            if (i == -1)
              System.out.println(val + " not found");
            else  
              System.out.println("Found " + val + " at " + i);
            break;
          
          default: 
            System.out.println("Invalid choice.");
            continue;
        }
      
        System.out.println(strList);
        System.out.println("Size = " + strList.size());
      }
    }
  }
}
